//遍历数组
function ThroughArray(arr){
    for (var i = 0 ; i<arr.length;i++){
        FindPictureClick(arr[i]);
    }
}

//遍历数组且存在
function ThroughArrayExists(arr){
    for (var i = 0 ; i<arr.length;i++){
        ClickIfExists(arr[i]);
    }
}

//判断是否有体力
function brain(){
    flag = FindPicture("没体力");
    return flag;
}

//路径和找图
function RoadAndFind(ImgName){
    ImgPath = images.read("/sdcard/Pictures/mrfz/" + ImgName + ".jpg");
    coordinate = findImage(captureScreen(),ImgPath,{
        region:[0,0,width,height]
    });
    return coordinate;
}

//返回主页
function ReturnMain(){
    arr = ["返回主页Step1","返回主页Step2"];
    ThroughArray(arr);
    sleep(3000);
}

//刷关等待时间
function LevelSleep(map){
    switch(map){
        case "剿灭":
            sleep(774000);
            while(true){
                sleep(2000);
                var value = FindPicture("剿灭行动结束");
                if(value){
                    sleep(5000);
                    break;
                }
            }
            flag = false;
            click(500,500);     //刷完后点击
            sleep(5000)
            click(500,500);  
            sleep(5000);
            break;
        default:
            sleep(60000);
            while(true){
                sleep(2000);
                var value = FindPicture("行动结束");
                if(value){
                    sleep(5000);
                    break;
                }
            }
            flag = false;
            click(500,500);     //刷完后点击
            sleep(5000);
            break;
    }
}


//使用药水
function UseMedicine(){
    while(true){
        FindPictureClick("体力加号");
        flag = FindPicture("没体力了");
        if(flag){
            FindPictureClick("没体力了");
            toastLog("准备开刷");
            break;
        }else{
            FindPictureClick("确认使用药水");
        }
    }
    flag = false;    
}


//坐标偏移  返回坐标
function CoordinateOffset(ImgName){
    var coordinate = RoadAndFind(ImgName);
    if (coordinate){
        return coordinate;
    }else{"没找到"+ImgName+"的坐标"};
}

//查找坐标  修改Flag
function FindPicture(ImgName){
    sleep(3000);
    coordinate = RoadAndFind(ImgName);
    if (coordinate){
        return flag = true;
    }
}

//遍历数组
function ThroughCoordinate(arr){
    for (var i = 0 ; i<arr.length;i++){
        ChangeCoordinate(arr[i]);
    }
}

//基建换岗
function ChangeCoordinate(ImgName){
    coordinate = RoadAndFind(ImgName);
    if(coordinate){
        click(coordinate.x,coordinate.y);
        return "Find";
    }else{
        return "NotFind";
    }
}

//查找坐标  找到则点击，找不到则继续运行
function ClickIfExists(ImgName){
    sleep(3000);
    coordinate = RoadAndFind(ImgName);
    if(coordinate){
        click(coordinate.x,coordinate.y);
        return "Find";
    }else{
         toastLog("未找到"+ImgName+"的坐标");
         return "NotFind";
    }
}

//查找坐标并点击    找不到则抛异常
function FindPictureClick(ImgName){
    sleep(3000);
    coordinate = RoadAndFind(ImgName);
    switch(ImgName){
        case "剿灭":
            if(coordinate){
                click(coordinate.x,coordinate.y);
                return "已进入剿灭";
            }else{
                toastLog("本周剿灭已打完");
                return "Finish";
            }
            break;
        case "代理指挥":
                if(coordinate){
                    click(coordinate.x,coordinate.y);
                }else{
                    toastLog("已选择代理指挥");
                    return "Finish";
                }
            break;
        //找到则点击，未找到则跳过
        //多个公告部分 未写
        case "访问下一位":
            if(coordinate){
                click(coordinate.x,coordinate.y);
            }
            break;
        default:
            click(coordinate.x,coordinate.y);
            break;
        
    }
}

//选择关卡
function FindInstanceZones(map){
    switch(map){
        case "1-7":
            FindPictureClick("终端");
            var arr = ["主题曲","幻灭","觉醒","黑暗时代","1-7"];
            ThroughArray(arr);
            break;
        case "红票":
            FindPictureClick("终端");
                //资源收集 红票 红票关卡
            var arr = ["资源收集","红票","红票关卡"];
            ThroughArray(arr);
            break;
        case "剿灭":
            FindPictureClick("终端");
            var ReturnValue = FindPictureClick("剿灭");
            return ReturnValue;
            break;  
        case "不清体力":
            break;
        case "前往上一次作战":
            FindPictureClick("终端");
            var arr = [map];
            ThroughArray(arr);
            break;
    }
}

function FindPicture2(){
    sleep(3000);
    coordinate = RoadAndFind("返回主页Step1");
    if (coordinate){
        return false;
    }
}
//开始刷关
function ClickStart(map){
    var arr = ["代理指挥","开始行动Step1","开始行动2"];
    ThroughArrayExists(arr);
    flag = brain();
    if(flag){
        click(300,300);
        sleep(2000);
        //活动没有返回主页ui，调用FP2如果没找到则随机点击调出ui
        if (!FindPicture2()){
            click(300,300);
        }
        sleep(1000);
        ReturnMain();
        return "没体力了";
    }else{
        FindPictureClick("开始行动Step2");
        LevelSleep(map);
        return null;
    }
}

//重复刷图
function Repeat(map){
    if(map!="不清体力"){
        var ReturnValue = FindInstanceZones(map);
        switch(map){
            case "剿灭":
                if(ReturnValue=="已进入剿灭"){
                    for (var i=0;i<5;i++){
                        var ReturnValue = ClickStart(map);
                        if(ReturnValue=="没体力了"){
                            flag = false;
                            break;
                        }
                        if(i==4){
                            ReturnMain();
                        }
                        sleep(2000);
                    }
                }
                break;
            default:
                while(true){
                    var ReturnValue = ClickStart(map);
                    if(ReturnValue=="没体力了"){
                        flag = false;
                        break;
                    }
                }
        }
    }
}

//领取任务奖励
function mission(){
    FindPictureClick("任务");
    if(ClickIfExists("收集全部")=="Find"){
        sleep(2000);
        FindPictureClick("确定领取任务");
    }
    FindPictureClick("周常任务")
    if(ClickIfExists("收集全部")=="Find"){
        sleep(2000);
        FindPictureClick("确定领取任务");
    }
    ReturnMain();
}

//参观好友
function VisitFriend(){
    var arr = ["好友","好友列表","访问基建"];
    ThroughArray(arr);
    sleep(3000);
    for(var i=0;i<10;i++){
        sleep(6000);
        var value = ClickIfExists("访问下一位");
        if (value == "NotFind"){
            break;
        }
    }
    sleep(5000);
    ReturnMain();
}

//信用商店
function CreditShop(){
    sleep(3000);
    var arr = ["采购中心","信用交易所"];
    ThroughArray(arr);
    arr = ["收取信用","确定领取任务"];
    ThroughArrayExists(arr)
    while(true){
        var value = ClickIfExists("招募卷");
        if (value == "NotFind"){
            ReturnMain();
            break;
        }else{
            var arr = ["购买","确定领取任务"];
            ThroughArray(arr);
        }
    }
}

//基建收菜
function BaseCollection(){
    sleep(5000);
    while(true){
        if(ClickIfExists("蓝色小铃铛")=="Find"){
            break;
        }
    }
    var arr = ["产物","订单","信赖"];
    ThroughArrayExists(arr);
    var value = ClickIfExists("蓝色亮铃铛");
    if(value=="NotFind"){
        click(500,300);
    }
    var arr = ["制造站","贸易中","加速箭头","最多","确认制造","收取","返回","返回"];
    ThroughArrayExists(arr);
}

//会客室
function room(){
    var arr = ["会客室Step1","贸易中"];
    ThroughArray(arr);
    sleep(1000);
    if(FindPicture("信用奖励")){
        FindPictureClick("返回");
    }
    flag = false;
    sleep(1000);
    var value = ClickIfExists("会客室添加干员");
    if(value == "Find"){
        ClickIfExists("工作状态");
        sleep(1000);
        click(800,300);
        click(800,600);
        sleep(200);
        FindPictureClick("确认");
        sleep(2000);
    }
    sleep(1000);
    arr = ["接收线索1","线索领取"];
    ThroughArrayExists(arr);
    sleep(5000);
    arr = ["线索关闭"];
    ThroughArrayExists(arr);
    arr = ["接收线索2","全部收取"];
    ThroughArrayExists(arr);
    arr = ["沙发","线索"];
    ThroughArray(arr);
    for(var i=1;i<=7;i++){
        var Clue = "线索" + i;
        var ClueNumber = "线索NO" + i ;
        FindPictureClick(Clue);
        var flag = FindPicture("线索已置入")
        if(!flag){
            var value = ClickIfExists(ClueNumber);
        }
        flag = false;
    }
    var value = ClickIfExists("解锁线索");
    if(value=="Find"){
        FindPictureClick("返回");
    }else{
        arr = ["返回","返回"];
        ThroughArray(arr);
    }
}


//制造站换岗
function MakeChangePeople(){
    var arr =["制造站11","制造站11","贸易中"];
    ThroughArrayExists(arr);
    arr = ["制造站1","制造站2","制造站3","制造站4"];
    ClickIfExists("制造站1");
    CleanPeople();
    ClickIfExists("制造站2");
    CleanPeople();
    ClickIfExists("制造站3");
    CleanPeople();
    ClickIfExists("制造站4");
    CleanPeople();
    ClickIfExists("制造站1");
    FindPeople();
    ClickIfExists("制造站2");
    FindPeople();
    ClickIfExists("制造站3");
    FindPeople();
    ClickIfExists("制造站4");
    FindPeople();
    arr = ["返回","返回"];
    ThroughArray(arr);
}

//贸易战换岗
function OrderChangePeople(){
    var arr =["贸易站","贸易站","贸易中"];
    ThroughArrayExists(arr);
    ClickIfExists("贸易站1");
    OrderCleanPeople()
    ClickIfExists("贸易站2");
    OrderCleanPeople()
    ClickIfExists("贸易站1");
    FindPeople2();
    ClickIfExists("贸易站2");
    FindPeople2();
    arr = ["返回","返回"];
    ThroughArray(arr);
}

//制造站清人
function CleanPeople(){
    var coordinate = CoordinateOffset("进驻人员");
    click(coordinate.x+50,coordinate.y+50);
    sleep(1000);
    var arr = ["清空选择","确认"];
    ThroughCoordinate(arr);
    sleep(3000);

}

//贸易战清人
function OrderCleanPeople(){
    var coordinate = CoordinateOffset("进驻人员2");
    click(coordinate.x+30,coordinate.y+60);
    sleep(1000);
    var arr = ["清空选择","确认"];
    ThroughCoordinate(arr);
    sleep(3000);

}

//制造站换岗寻找干员
function FindPeople(){
    var value = FindPicture("制造站录像带")
    coordinate = CoordinateOffset("进驻人员");
    click(coordinate.x+50,coordinate.y+50);
    sleep(1000);
    if(value){
        arr = ["基建技能","录像带小车","食铁兽","霜叶","红豆","红豆2","白雪","白雪2","白面鸮","白面鸮2","杰西卡","调香师","调香师2","调香师3","史都华德","香草","泡普卡","芬","克洛斯","稀音","豆苗","刻尔柏","赫默","赫默2","夜刀","野鬃","流星","流星2","蛇屠箱","黑角","卡缇","米格鲁"];
        ThroughCoordinate(arr);
        sleep(500);
        swipe(1800,540,590,540,[2000]);
        sleep(500);
        ThroughCoordinate(arr);
        sleep(500);
        swipe(1800,540,590,540,[2000]);
        sleep(500);
        ThroughCoordinate(arr);
        sleep(500);
        swipe(1800,540,590,540,[2000]);
        sleep(500);
        ThroughCoordinate(arr);
        arr = ["确认"];
        ThroughArray(arr);
    }else{
        arr = ["基建技能","砾","夜烟","斑点","白面鸮","白面鸮2","杰西卡","调香师","调香师2","调香师3","史都华德","香草","泡普卡","芬","克洛斯","稀音","刻尔柏","豆苗","赫默","赫默2","夜刀","野鬃","流星","流星2","蛇屠箱","黑角","卡缇","米格鲁"];
        ThroughCoordinate(arr);
        sleep(500);
        swipe(1800,540,590,540,[2000]);
        sleep(500);
        ThroughCoordinate(arr);
        sleep(500);
        swipe(1800,540,590,540,[2000]);
        sleep(500);
        ThroughCoordinate(arr);
        sleep(500);
        swipe(1800,540,590,540,[2000]);
        sleep(500);
        ThroughCoordinate(arr);
        arr = ["确认"];
        ThroughArray(arr);
    }
    flag = false;
    sleep(3000);
}

//贸易战换岗寻找干员
function FindPeople2(){
    sleep(1000);
    var coordinate = CoordinateOffset("进驻人员2");
    click(coordinate.x+30,coordinate.y+60);
    sleep(1000);
    arr = ["月见夜","月见夜2","夜刀","芬","夜烟","巫恋","空爆","古米","缠丸","慕斯","安比尔","梓兰","远山","剑圣","雪锥","可颂","银灰","可颂","孑","剑圣2","蛇屠箱","银灰2"];
    ThroughCoordinate(arr);
    sleep(500);
    swipe(1800,540,590,540,[2000]);
    sleep(500);
    ThroughCoordinate(arr);
    arr = ["确认"];
    ThroughArray(arr);
}

//宿舍换人
function accomodation(){
    sleep(1000);
    click(1000,450);
    ChangePeopleAccomodation()
    click(1200,550);
    ChangePeopleAccomodation()
    click(1000,750);
    ChangePeopleAccomodation()
    click(1200,950);
    ChangePeopleAccomodation()
    function ChangePeopleAccomodation(){
        sleep(1000);
        var arr = ["进驻信息","清空","添加人"];
        ThroughArrayExists(arr);
        sleep(1000);
        click(800,300);
        click(800,600);
        click(1000,300);
        click(1000,600);
        click(1200,300);
        ClickIfExists("确认");
        ClickIfExists("工作中");
        arr = ["返回"];
        ThroughArray(arr);
        sleep(1000);
    }
}

//发电站
function power(){
    while(true){
        var value = ClickIfExists("发电站01");
        if(value=="Find"){
            var arr = ["进驻信息","清空","添加人","工作状态"];
            ThroughArrayExists(arr);
            sleep(1000);
            click(800,300);
            ClickIfExists("确认");
            ClickIfExists("返回");
        }else{
            break;
        }
    }
}

//办公室
function office(){
    var arr = ["办公室","人力资源"];
    ThroughArray(arr);
    sleep(1000);
    arr = ["工作状态"];
    ThroughArrayExists(arr);
    sleep(1000);
    click(800,600);
    arr = ["确认","返回"];
    ThroughArray(arr);
}

//基建
function base(){
    FindPictureClick("基建");
    BaseCollection();
    // ReturnMain();
}

function change(){
    //宿舍换人
    accomodation();

    //制造站换人
    MakeChangePeople();

    //贸易战换人
    OrderChangePeople();

    //办公室换人
    office();

    //发电站换人
    power();

    //会客室换人
    room();
    
    //返回主页
    ReturnMain();

}

function recruit(){
    var arr = ["公招"];
    ThroughArray(arr);
    var arr = ["公招","聘用候选人","SKIP"];
    while(true){
        var value = ClickIfExists("聘用候选人")
        if(value=="Find"){
            ClickIfExists("SKIP")
            sleep(7000);
            click(600,600);
        }else{
            FindPictureClick("返回");
            break;
        }
    }
}

//main

//定义变量
//屏幕高度
var height = device.height;
//屏幕宽度
var width = device.width;
//读取的图片路径
var ImgPath;
//操作名称
var ImgName;
//   flag!!!
var flag = false;
//DATA对象
var myDate = new Date(); 

//截图权限
toastLog("@author Sayaaa");
if (!requestScreenCapture(false)) {
    toastLog("请求截图失败");
    exit();
}


// 交互部分

//选图UI
var options = ["剿灭","1-7","不清体力","前往上一次作战"]
// 先选项不包含红票
// 满足case替换选项
switch(myDate.getDay()){
    case 0:
    case 1:
        options = ["红票","剿灭","1-7","不清体力","前往上一次作战"];
    case 4:
    case 6:
        //判断是否大于4点
        if(myDate.getHours()>=4){
            options = ["红票","剿灭","1-7","不清体力","前往上一次作战"];
        }
        break;
    case 2:
    case 5:
        //判断是否小于4点
        if(myDate.getHours()<4){
            options = ["红票","剿灭","1-7","不清体力","前往上一次作战"];
        }
    default:
        break;
 }
var i = dialogs.select("请选择要打的关卡", options);
if(i >= 0){
    toastLog("您选择的是" + options[i]);
}else{
    toastLog("您取消了选择");
}
//多选框UI



//调用function

//使用体力药


//刷关
switch(options[i]){
    case undefined:
    case "不清体力":
        break;
    default:
        Repeat(options[i]);
}


//参观好友基建
VisitFriend();

//访问商店
CreditShop();

//收菜
base();

//换人
change();

//公招
recruit()

//领任务
mission();

sleep(3000);
toastLog("@author Sayaaa");